package net.xgclass.online_xgclass.controller;

import com.github.pagehelper.PageInfo;
import net.xgclass.online_xgclass.model.entity.User;
import net.xgclass.online_xgclass.model.request.LoginRequest;
import net.xgclass.online_xgclass.service.UserService;
import net.xgclass.online_xgclass.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/v1/pri/user")
public class UserController {



    @Autowired
    private UserService userService;

    /**
     * 注册接口
     * @param userInfo
     * @return
     */
    @PostMapping("register")
    public JsonData register(@RequestBody Map<String,String> userInfo ){

        int rows = userService.save(userInfo);

        return rows == 1 ? JsonData.buildSuccess(): JsonData.buildError("注册失败，请重试");

    }


    /**
     * 登录接口  前台
     * @param loginRequest
     * @return
     */
    @PostMapping("login")
    public JsonData login(@RequestBody LoginRequest loginRequest){

        String token = userService.findByPhoneAndPwd(loginRequest.getPhone(), loginRequest.getPwd());

        return token == null ?JsonData.buildError("登录失败，账号密码错误"): JsonData.buildSuccess(token);

    }



    //***********************************后台***********************************************//


    /**
     * 登录接口  后台
     * @param loginRequest
     * @return
     */
    @PostMapping("loginManage")
    public JsonData loginManage(@RequestBody LoginRequest loginRequest){
        String token = userService.findByUserAndPwd(loginRequest.getName(), loginRequest.getPwd());
        return token==null?JsonData.buildError("用户名密码错误"):JsonData.buildSuccess(token);
    }


    /**
     * 根据用户id查询用户信息
     * @param request
     * @return
     */
    @GetMapping("find_by_token")
    public JsonData findUserInfoByToken(HttpServletRequest request){

        Integer userId = (Integer) request.getAttribute("user_id");

        if(userId == null){
            return JsonData.buildError("查询失败");
        }

        User user =  userService.findByUserId(userId);

        return JsonData.buildSuccess(user);

    }

    // /**
    //  * 查询用户列表  后台
    //  * @return
    //  */
    // @RequestMapping(
    //         value = {"findUser"},
    //         method = {RequestMethod.GET}
    // )
    // public JsonData findUser(){
    //     List<User> user = userService.findUser();
    //     if (user !=null){
    //         return JsonData.buildSuccess(user);
    //     }else {
    //         return JsonData.buildError(-1,"查询失败");
    //     }
    //
    // }
    /**
     * 查询用户列表  后台
     * @return
     */
    @RequestMapping(
            value = {"findUser"},
            method = {RequestMethod.GET}
    )
    public PageInfo<User> pageInfo( int pagenum,int pagesize,String byname){

        return userService.findUser(pagenum,pagesize,byname);
    }


    /**
     * 通过用户id删除用户  后台
     * @param id
     * @return
     */
    @RequestMapping(
            value = {"/{id}"},
            method ={RequestMethod.DELETE}
    )
    public JsonData deleteById(@PathVariable Integer id){
        int flag = userService.deleteById(id);
        if (flag==1){
            return JsonData.buildSuccess();
        }else {
            return JsonData.buildError("删除失败");
        }
    }

    /**
     * 添加用户  后台
     * @param user
     * @return
     */
    @RequestMapping(
            value = {"addUser"},
            method = {RequestMethod.POST}
    )
    public JsonData addUser(@RequestBody User user){
        int flag = userService.addUser(user);
        if (flag==1){
            return JsonData.buildSuccess();
        }else{
            return JsonData.buildError("添加失败");
        }
    }

    /**
     * 修改用户信息  后台
     * @param user
     * @return
     */
    @RequestMapping(
            value = {"/edit"},
            method = {RequestMethod.PUT}
    )
    public JsonData updateById(@RequestBody User user){
        int flag = userService.updateById(user);
        return flag==1?JsonData.buildSuccess():JsonData.buildError("修改失败");

    }


}
